package FIT::Fixture::Summary;

use FIT::Class;
use IO::All -utf8;
use FIT::Document::Transformer;

extends 'FIT::Fixture::Base';

override do_table  => sub {
    my $table        = shift;
    my $context      = $table->fit_context;
    my $input_update = localtime io($context->in_file)->mtime;
    my $run_date     = localtime $context->run_date;
    my $elapsed      = compute_elapsed( time - $context->run_date );

    transform { AddRows rows are [
        ['counts'           => $context->as_summary_line],
        ['input file'       => $context->in_file. ''    ], # Twig goes crazy when given IO::All objects
        ['input update'     => $input_update            ],
        ['output file'      => $context->out_file. ''   ],
        ['run dir'          => $context->run_dir. ''    ],
        ['run date'         => $run_date                ],
        ['run elapsed time' => $elapsed                 ],
    ] } $table;

    # don't use Right and Wrong, because we don't want these counted!
    transform { $context->is_ok? Green: Red } $table->second_row->last_cell;

    # add link to in_file
    transform { AddLink href is $context->in_file_relative_to_out_file }
        $table->second_row->next_row->last_cell;

    # add result box to top of page so users don't need to scroll down
    transform { AddBodyHeader context is $context } $table->document_body;
};

sub compute_elapsed() {
    my $t = shift;
    my $s = $t % 60;
    my $m = ( ($t - $s) % 3600 ) / 60;
    my $h = int( $t / 3600 );
    return ($h? "$h:": ""). sprintf("%02d.%02d", $m, $s);
}

